<!-- S search -->
<app-search-bar
  [submitLoading]="tableParams.loading"
  [searchOptions]="searchOptions"
  labelFlex="90px"
  (onSearch)="queryData($event)"
  (onReset)="resetData($event)"
>
</app-search-bar>
<!-- E search -->

<nz-card class="m-t-2">
  <div class="operation-wrap">
    <button
      *ngIf="permission.userPermission.has('secondCoupon:add')"
      nz-button
      nzType="primary"
      (click)="jumpPage(0)"
    >新增优惠券</button>
  </div>

  <div class="pagination-wrap-position table-wrap p-t-10">
    <nz-table
      #basicTable
      nzSize="small"
      nzShowSizeChanger
      nzShowQuickJumper
      nzOuterBordered
      [nzScroll]="{ x: '1100px' }"
      [nzFrontPagination]="false"
      [nzLoadingDelay]="100"
      [nzLoading]="tableParams.loading"
      [nzData]="tableParams.tableData"
      [nzPageSizeOptions]="[5, 10, 20, 50, 100]"
      [nzTotal]="tableParams.total"
      [nzPageIndex]="tableParams.page"
      [nzPageSize]="tableParams.pageSize"
      [nzShowTotal]="totalTemplate"
      (nzPageIndexChange)="onPageIndexChange($event)"
      (nzPageSizeChange)="onPageSizeChange($event)"
    >
      <thead>
        <tr>
          <th nzAlign="center" nzWidth="180px" nzLeft>优惠券ID</th>
          <th nzAlign="center" nzWidth="220px">优惠券名称</th>
          <th nzAlign="center" nzWidth="160px">类型</th>
          <th nzAlign="center" nzWidth="200px">适用品类</th>
          <th nzAlign="center" nzWidth="120px">适用用户</th>
          <th nzAlign="center" nzWidth="120px">前端展示</th>
          <th nzAlign="center" nzWidth="120px">已领取</th>
          <th nzAlign="center" nzWidth="120px">剩余</th>
          <th nzAlign="center" nzWidth="200px">备注</th>
          <th nzAlign="center" nzWidth="150px">状态</th>
          <th nzAlign="center" nzWidth="380px" nzRight>操作</th>
        </tr>
      </thead>

      <tbody>
        <tr *ngFor="let data of basicTable.data; let index = index;">
          <!-- 优惠券ID -->
          <td nzAlign="center" nzLeft>{{ data.id }}</td>
          <!-- 优惠券名称 -->
          <td nzAlign="center">{{ data.name || '-' }}</td>
          <!-- 类型 -->
          <td nzAlign="center">
            {{ data.type === 1 ? '满减券' : '折扣券' }}
          </td>
          <!-- 适用品类 -->
          <td nzAlign="center">
            {{ categoryTypeFilter(data.containsTypes) }}
          </td>
          <!-- 适用用户 -->
          <td nzAlign="center">
            <div [ngSwitch]="data.userType">
              <div *ngSwitchCase="0">
                <nz-tag>通用</nz-tag>
              </div>
              <div *ngSwitchCase="1">
                <nz-tag [nzColor]="'#87d068'">C端</nz-tag>
              </div>
              <div *ngSwitchCase="2">
                <nz-tag [nzColor]="'#2db7f5'">B端</nz-tag>
              </div>
              <div *ngSwitchDefault>-</div>
            </div>
          </td>
          <!-- 前端展示 -->
          <td nzAlign="center">
            <span [ngStyle]="{'color': data.isShowWeb === 0 ? '#F56C6C' : ''}">
              {{ data.isShowWeb === 0 ? '不展示' : '展示' }}
            </span>
          </td>
          <!-- 已领取 -->
          <td nzAlign="center">
            <span [ngStyle]="{'color': '#2e8ff4'}">
              {{ data.numberReceived || '0' }}
            </span>
          </td>
          <!-- 剩余 -->
          <td nzAlign="center">
            <span [ngStyle]="{'color': data.remainingNumber < 1000 ? '#ff0052' : ''}">
              {{ data.remainingNumber || '0' }}
            </span>
          </td>
          <!-- 备注 -->
          <td nzAlign="center">
            {{ data.remark || '-' }}
          </td>
          <!-- 状态 -->
          <td nzAlign="center">
            <span
              *ngIf="data.status"
              [ngStyle]="{'color': data.status | customizedCouponStatus: 'color'}">
              {{ data.status | customizedCouponStatus: 'label' }}
            </span>
          </td>
          <!-- 操作 -->
          <td nzAlign="center" nzRight class="opreation-btns">
            <a
              nz-button
              nzSize="small"
              nzType="link"
              (click)="jumpPage(2, data)"
            >查看</a>

            <a
              *ngIf="[5, 7].includes(data.status)"
              nz-button
              nzSize="small"
              nzType="link"
              (click)="jumpPage(1, data)"
            >编辑</a>

            <a
              nz-button
              nzSize="small"
              nzType="link"
              (click)="jumpPage(3, data)"
            >复制</a>

            <a
              *ngIf="[6, 7].includes(data.status)"
              nz-button
              nzSize="small"
              nzType="link"
              (click)="showExamineModal(data)"
            >审核</a>

            <a
              *ngIf="[1, 2].includes(data.status)"
              nz-button
              nzSize="small"
              nzType="link"
              nzDanger
              nz-popconfirm
              nzPopconfirmTitle="确定停止该优惠券?"
              (nzOnConfirm)="stopItem(data)"
            >停止</a>

            <a
              *ngIf="[4].includes(data.status)"
              nz-button
              nzSize="small"
              nzType="link"
              nz-popconfirm
              nzPopconfirmTitle="确定启用该优惠券?"
              (nzOnConfirm)="enableItem(data)"
            >启用</a>

            <a
              *ngIf="[1, 5].includes(data.status)"
              nz-button
              nzSize="small"
              nzType="link"
              nzDanger
              nz-popconfirm
              nzPopconfirmTitle="确认删除?"
              (nzOnConfirm)="deleteItem(data)"
            >删除</a>

            <a
              *ngIf="[2].includes(data.status)"
              nz-button
              nzSize="small"
              nzType="link"
              (click)="showCouponModal(data)"
            >手动发放</a>

            <a
              *ngIf="[2, 3, 4].includes(data.status)"
              nz-button
              nzSize="small"
              nzType="link"
              (click)="showUserCouponModal(data)"
            >用户优惠券</a>
          </td>
        </tr>
      </tbody>
    </nz-table>

    <!-- 分页template -->
    <ng-template #totalTemplate let-total>共有 {{ total }} 条</ng-template>
  </div>
</nz-card>

<!-- S 审核Modal -->
<nz-modal
  nzClassName="custom-model-wrap"
  [nzFooter]="null"
  [(nzVisible)]="isExamineVisible"
  [nzTitle]="'审核'"
  (nzOnCancel)="isExamineVisible = false">

  <div *nzModalContent>
    <div class="custom-model-body">
      <nz-textarea-count [nzMaxCharacterCount]="100">
        <textarea rows="4" nz-input placeholder="请输入审核意见" [(ngModel)]="examineParams.remark"></textarea>
      </nz-textarea-count>
    </div>

    <div class="custom-model-foot">
      <div class="custom-foot-cont">
        <button nz-button nzType="primary" (click)="subExamine(1)">审核通过</button>
        <button nz-button nzType="primary" nzDanger (click)="subExamine(7)">审核不通过</button>
      </div>
    </div>
  </div>
</nz-modal>
<!-- E 审核Modal -->

<!-- S 手动发放 -->
<manual-release-modal
  [(ngModel)]="releaseVisible"
  [couponCode]="opreaItem?.no"
  [modalTitle]="'手动发放优惠券'"
  (submitManual)="submitManual($event)"
></manual-release-modal>
<!-- E 手动发放 -->

<!-- S 用户优惠券 -->
<nz-modal
  nzClassName="custom-model-wrap"
  [nzFooter]="null"
  [nzWidth]="1300"
  [(nzVisible)]="isUserVisible"
  [nzTitle]="'用户优惠券'"
  (nzOnCancel)="isUserVisible = false">

  <div *nzModalContent>
    <div class="custom-model-body">
      <div class="modal-search-wrap">
        <!-- S search -->
        <app-search-bar
          [submitLoading]="userCouponTableParams.loading"
          [searchOptions]="userCouponSearch"
          labelFlex="90px"
          [xxlSize]="8"
          (onSearch)="couponQueryResetData($event)"
          (onReset)="couponQueryResetData($event)"
        ></app-search-bar>
        <!-- E search -->
      </div>

      <div class="opera-wrap p-t-15">
        <button
          nz-button
          nzType="primary"
          (click)="batchWithdrawCoupon()"
        >批量撤回</button>
      </div>

      <div class="table-wrap p-t-10">
        <nz-table
          #userTable
          nzSize="small"
          nzShowSizeChanger
          nzShowQuickJumper
          nzBordered
          [nzScroll]="{ x: '1100px' }"
          [nzFrontPagination]="false"
          [nzLoadingDelay]="100"
          [nzLoading]="userCouponTableParams.loading"
          [nzData]="userCouponTableParams.tableData"
          [nzPageSizeOptions]="[5, 10, 20, 50, 100]"
          [nzTotal]="userCouponTableParams.total"
          [nzPageIndex]="userCouponTableParams.page"
          [nzPageSize]="userCouponTableParams.pageSize"
          [nzShowTotal]="totalTemplate2"
          (nzPageIndexChange)="userIndexChange($event)"
          (nzPageSizeChange)="userSizeChange($event)"
          (nzCurrentPageDataChange)="onCurrentPageDataChange($event)"
        >
          <thead>
            <tr>
              <th
                [(nzChecked)]="checked"
                [nzIndeterminate]="indeterminate"
                nzWidth="50px"
                nzLeft
                (nzCheckedChange)="onAllChecked($event)"
              ></th>
              <th nzAlign="center" nzWidth="165px">优惠券ID</th>
              <th nzAlign="center" nzWidth="165px">用户优惠券ID</th>
              <th nzAlign="center" nzWidth="120px">用户ID</th>
              <th nzAlign="center" nzWidth="165px">领取时间</th>
              <th nzAlign="center" nzWidth="165px">过期时间</th>
              <th nzAlign="center" nzWidth="180px">关联单号</th>
              <th nzAlign="center" nzWidth="165px">使用时间</th>
              <th nzAlign="center" nzWidth="120px">状态</th>
              <th nzAlign="center" nzWidth="120px" nzRight>操作</th>
            </tr>
          </thead>
  
          <tbody>
            <tr *ngFor="let data of userTable.data; let index = index;">
              <td
                nzLeft
                [nzChecked]="setOfCheckedId.has(data.id)"
                (nzCheckedChange)="onItemChecked(data.id, $event)"
              ></td>
              <!-- 优惠券ID -->
              <td nzAlign="center">{{ data.couponNo }}</td>
              <!-- 用户优惠券ID -->
              <td nzAlign="center">{{ data.id }}</td>
              <!-- 用户ID -->
              <td nzAlign="center">
                <a
                  nz-button
                  nzType="link"
                  nzSize="small"
                  target="_blank"
                  [routerLink]="['/customer/tabs', data.realUserId, 0]"
                >{{ data.realUserId }}</a>
              </td>
              <!-- 领取时间 -->
              <td nzAlign="center">{{ (data.getTime | date: 'yyyy-MM-dd HH:mm:ss') || '-' }}</td>
              <!-- 过期时间 -->
              <td nzAlign="center">{{ (data.endTime | date: 'yyyy-MM-dd HH:mm:ss') || '-' }}</td>
              <!-- 关联单号 -->
              <td nzAlign="center">{{ data.orderNum || '-' }}</td>
              <!-- 使用时间 -->
              <td nzAlign="center">{{ (data.useTime | date: 'yyyy-MM-dd HH:mm:ss') || '-' }}</td>
              <!-- 状态 -->
              <td nzAlign="center">
                <span
                  *ngIf="data.status || data.status == 0"
                  [ngStyle]="{'color': data.status | adzUserCouponStatus: 'color'}"
                >
                  {{ data.status | adzUserCouponStatus: 'label' }}
                </span>
              </td>
              <!-- 操作 -->
              <td nzAlign="center" nzRight>
                <a
                  [disabled]="data.status !== 1"
                  nz-button
                  nzType="link"
                  nz-popconfirm
                  nzPopconfirmTitle="确认撤回?"
                  (nzOnConfirm)="withdrawCoupon(data)"
                >
                  撤回
                </a>
              </td>
            </tr>
          </tbody>
        </nz-table>
  
        <!-- 分页template -->
        <ng-template #totalTemplate2 let-total>共有 {{ total }} 条</ng-template>
      </div>
    </div>

    <!-- <div class="custom-model-foot">
      <div class="custom-foot-cont">
        <button nz-button nzType="default" (click)="isUserVisible = false">取消</button>
        <button nz-button nzType="primary" [nzLoading]="userLoading">确定</button>
      </div>
    </div> -->
  </div>
</nz-modal>
<!-- E 用户优惠券 -->